package com.android.love.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	private static final String DB_NAME = "love_db";
	private static final int DB_VERSION = 1;
	private SQLiteDatabase mDb;

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// Fav表
		creatCartTable(db);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	/**
	 * 创建购物车数据库
	 * 
	 * @param db
	 */
	private void creatCartTable(SQLiteDatabase db) {
		StringBuilder favStr = new StringBuilder();
		favStr.append("CREATE TABLE IF NOT EXISTS ")
				.append(CartColumns.TABLE_NAME).append(" ( ")
				.append(CartColumns._ID)
				.append(" INTEGER PRIMARY KEY AUTOINCREMENT,")
				.append(CartColumns.GOODS_ID).append(" Long,")
				.append(CartColumns.GOODS_IMAGE).append(" varchar(300),")
				.append(CartColumns.GOODS_NAME).append(" varchar(300),")
				.append(CartColumns.PRICE).append(" Float,")
				.append(CartColumns.OLD_PRICE).append(" Float,")
				.append(CartColumns.COLOR).append(" varchar(100),")
				.append(CartColumns.SIZE).append(" varchar(300),")
				.append(CartColumns.FORMAT).append(" varchar(100));");
		db.execSQL(favStr.toString());
	}

	/**
	 * 获取数据库操作对象
	 * 
	 * @param isWrite
	 *            是否可写
	 * @return
	 */
	public synchronized SQLiteDatabase getDatabase(boolean isWrite) {

		if (mDb == null || !mDb.isOpen()) {
			if (isWrite) {
				try {
					mDb = getWritableDatabase();
				} catch (Exception e) {
					// 当数据库不可写时
					mDb = getReadableDatabase();
					return mDb;
				}
			} else {
				mDb = getReadableDatabase();
			}
		}
		// } catch (SQLiteException e) {
		// // 当数据库不可写时
		// mDb = getReadableDatabase();
		// }
		return mDb;
	}

	public int delete(String table, String whereClause, String[] whereArgs) {
		getDatabase(true);
		return mDb.delete(table, whereClause, whereArgs);
	}

	public long insert(String table, String nullColumnHack, ContentValues values) {
		getDatabase(true);
		return mDb.insertOrThrow(table, nullColumnHack, values);
	}

	public int update(String table, ContentValues values, String whereClause,
			String[] whereArgs) {
		getDatabase(true);
		return mDb.update(table, values, whereClause, whereArgs);
	}

	public Cursor rawQuery(String sql, String[] selectionArgs) {
		getDatabase(false);
		return mDb.rawQuery(sql, selectionArgs);
	}

	public void execSQL(String sql) {
		getDatabase(true);
		mDb.execSQL(sql);
	}

	public Cursor query(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			// final
			String orderBy) {
		getDatabase(false);
		return mDb.query(table, columns, selection, selectionArgs, groupBy,
				having, orderBy);
	}

}
